TOOL.Category		= "Construction";
TOOL.Name			= "#Laser";
TOOL.Command		= nil;
TOOL.ConfigName		= "";

TOOL.ClientConVar = { 
	r			= 255,
	g			= 0,
	b			= 0,
	a			= 255,
	dmg			= 100,
	width		= 1,
	mat			= "sprites/laserbeam.spr",
	noise		= 0,
	scroll		= 35,
	dt			= -1
};

if CLIENT then
	language.Add( "Tool_ravlaser_name", "Laser" );
	language.Add( "Tool_ravlaser_desc", "Spawns laser" );
	language.Add( "Tool_ravlaser_0", "Click to one point, where laser starts,  and then point where laser ends" );
	language.Add( "Undone_RavLaser", "Undone laser" );
	language.Add( "Cleanup_RavLaser", "Lasers" );
	language.Add( "Cleaned_RavLaser", "Cleaned up Lasers" );
end
	
if SERVER then
	CreateConVar("sbox_maxravlasers",50);
end

list.Set( "RavLaserMaterials", {}, "cable/redlaser.vmt" );
list.Set( "RavLaserMaterials", {}, "sprites/bluelaser1.spr" );
list.Set( "RavLaserMaterials", {}, "effects/redlaser1.vmt" );
list.Set( "RavLaserMaterials", {}, "cable/physbeam.spr" );
list.Set( "RavLaserMaterials", {}, "cable/xbeam2.spr" );
list.Set( "RavLaserMaterials", {}, "sprites/bluelight1.spr" );
list.Set( "RavLaserMaterials", {}, "cable/hydra.vmt" );
list.Set( "RavLaserMaterials", {}, "cable/crystal_beam1.spr" );
list.Set( "RavLaserMaterials", {}, "sprites/laserbeam.spr" );

cleanup.Register("RavLaser");

/*-----------------------------------------------------------------------*
 * Gets a table containing tool options.
 *-----------------------------------------------------------------------*/
function TOOL:GetOptions()
	local options = { };
	options["r"]			= self:GetClientNumber("r", 255);
	options["g"]			= self:GetClientNumber("g", 0);
	options["b"]			= self:GetClientNumber("b", 0);
	options["a"]			= self:GetClientNumber("a", 255);
	options["dmg"]			= self:GetClientNumber("dmg", 100);
	options["width"]		= self:GetClientNumber("width", 1);
	options["mat"]			= self:GetClientString("mat", "sprites/laserbeam.spr");
	options["noise"]		= self:GetClientNumber("noise", 0);
	options["scroll"]		= self:GetClientNumber("scroll", 35);
	options["dt"]			= self:GetClientNumber("dt", -1);
	return options;
end

function TOOL:LeftClick( trace )

	if trace.Entity && trace.Entity:IsPlayer() then return false end;
	if (CLIENT) then return true end;
	if ( not self:GetOwner():CheckLimit( "ravlasers" ) ) then return false end;
	
	if self.LastPos ~= nil then
		SpawnLaser(self.LastPos, trace.HitPos, self);
		self.LastPos = nil;
	else
		self.LastPos = trace.HitPos;
	end
	
	return true

end

function TOOL:RightClick( trace )

	return self:LeftClick( trace );

end

function SpawnLaser ( pos1, pos2, self )
	local BasePoint = ents.Create("info_target");
	BasePoint:SetPos(pos1);
	BasePoint:SetKeyValue("targetname", "RavLaser_Bases_" .. BasePoint:EntIndex());
	BasePoint:Spawn();
	local RavLaser = ents.Create("env_laser");
	RavLaser:SetPos(pos2);
	RavLaser:SetKeyValue("rendercolor", self:GetClientInfo( "r" ) .. " " .. self:GetClientInfo( "g" ) .. " " .. self:GetClientInfo( "b" ));
	RavLaser:SetKeyValue("renderamt", self:GetClientInfo( "a" ));
	RavLaser:SetKeyValue("TextureScroll", self:GetClientInfo( "scroll" ));
	RavLaser:SetKeyValue("targetname", "RavLaser_" .. RavLaser:EntIndex());
	RavLaser:SetKeyValue("parentname", "");
	RavLaser:SetKeyValue("damage", self:GetClientInfo( "dmg" ));
	RavLaser:SetKeyValue("texture", self:GetClientInfo( "mat" ));
	RavLaser:SetKeyValue("spawnflags", "1");
	RavLaser:SetKeyValue("NoiseAmplitude", self:GetClientInfo( "noise" ));
	RavLaser:SetKeyValue("width", self:GetClientInfo( "width" ));
	RavLaser:SetKeyValue("dissolvetype", self:GetClientInfo( "dt"));
	RavLaser:SetKeyValue("EndSprite", "");
	RavLaser:SetKeyValue("LaserTarget", "RavLaser_Bases_" .. BasePoint:EntIndex());
	RavLaser:SetKeyValue("TouchType", "0");
	RavLaser:Spawn();
	
	undo.Create("RavLaser");
    undo.AddEntity(RavLaser);
	undo.AddEntity(BasePoint);
    undo.SetPlayer(self:GetOwner());
	undo.Finish();
	
	self:GetOwner():AddCleanup("RavLaser", RavLaser);
end 
/*-----------------------------------------------------------------------*
 * Builds the tool's control panel.
 *-----------------------------------------------------------------------*/
function TOOL.BuildCPanel(panel)

	local params = { Label = "#Presets", MenuButton = 1, Folder = "ravlaser", Options = {}, CVars = {} };
	params.Options.default = {
				ravlaser_r			= 255,
				ravlaser_g			= 0,
				ravlaser_b			= 0,
				ravlaser_a			= 255,
				ravlaser_dmg		= 100,
				ravlaser_width		= 1,
				ravlaser_mat		= "sprites/laserbeam.spr",
				ravlaser_noise		= 0,
				ravlaser_scroll		= 35,
				ravlaser_dt			= -1,
				}
		table.insert( params.CVars, "ravlaser_r" );
 		table.insert( params.CVars, "ravlaser_g" );
 		table.insert( params.CVars, "ravlaser_b" );
 		table.insert( params.CVars, "ravlaser_a" );
 		table.insert( params.CVars, "ravlaser_dmg" );
 		table.insert( params.CVars, "ravlaser_width" );
		table.insert( params.CVars, "ravlaser_mat" );
		table.insert( params.CVars, "ravlaser_noise" );
		table.insert( params.CVars, "ravlaser_scroll" );
		table.insert( params.CVars, "ravlaser_dt" );
 	panel:AddControl( "ComboBox", params );

	panel:AddControl( "Color", 		{	Label = "A Color of laser",
										Red = "ravlaser_r",
										Blue = "ravlaser_b",
										Green = "ravlaser_g",
										Alpha = "ravlaser_a",
										ShowHSV = 1,
										ShowRGB = 1,
										Multiplier = 255 } );
										
	panel:AddControl( "MatSelect", {	Label = "Material",
										Height = 1,
										ItemWidth = 24, 
										ItemHeight = 64, 
										ConVar = "ravlaser_mat",
										Options = list.Get( "RavLaserMaterials" ) } );
										
	panel:AddControl( "Slider", 	{	Label	= "#Laser Noise",
										Type	= "Integer",
										Min		= "0",
										Max		= "255",
										Command	= "ravlaser_noise" } );
										
	panel:AddControl( "Slider", 	{	Label	= "#Laser Texture Scroll Rate",
										Type	= "Integer",
										Min		= "0",
										Max		= "100",
										Command	= "ravlaser_scroll" } );
										
	panel:AddControl( "Slider",		{	Label	= "#Damage",
										Type	= "Float",
										Min		= "0",
										Max		= "10000",
										Command	= "ravlaser_dmg" } );
										
	panel:AddControl( "Slider", 	{	Label	= "#Laser width",
										Type	= "Float",
										Min		= "0",
										Max		= "100",
										Command	= "ravlaser_width" } );
	local params = {Label = "Dissolve Type", MenuButton = "0", Options = {}}
	params.Options["None"] = {ravlaser_dt = "-1"}
	params.Options["Energy"] = {ravlaser_dt = "0"}
	params.Options["Heavy Electrical"] = {ravlaser_dt = "1"}
	params.Options["Light Electrical"] = {ravlaser_dt = "2"}
	panel:AddControl( "ComboBox", params )
	
	
end